#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Solution {
public:
  string reverseWords(string s) {
    string result = "";

    // strip blank
    bool notBlank;
    int length = s.size();
    int start = 0;
    while (start < length && s[start] == ' ') {
      start++;
    }
    int end = s.size() - 1;
    while (end >= start && s[end] == ' ') {
      end--;
    }

    // merge blank
    char lastIsSpace = false;
    for (int i = start; i <= end; i++) {
      if (s[i] == ' ') {
        if (!lastIsSpace) {
          result.push_back(s[i]);
        }

        lastIsSpace = true;
      } else {
        lastIsSpace = false;
        result.push_back(s[i]);
      }
    }

    string::iterator it = s.begin();
    while (it != s.end()){
      it++;
      if (*it == ' '){
	
      }
      
    }

    
    vector<string> splited;
    bool lastSpace = true;
    int curStart = 0;
    int curEnd = 0;
    string tmp;
    for (int i=0;i<s.size();i++){
      if (s[i]==' '){
	lastSpace = true;
	continue;
      }

      if (lastSpace){
	curStart = i;
      }else {
	curEnd = i;
      }
      
    }

    
    // partial reverse

    // whole reverse

    return result;
  }

  string reverse(string s) {
    string result;
    result.assign(s);
    int lo = 0;
    int hi = s.size() - 1;
    while (lo < hi) {
      char tmp = result[lo];
      result[lo] = result[hi];
      result[hi] = tmp;
      lo++;
      hi--;
    }
    return result;
  }
};

void testcase01() {
  printf("\n%s running!\n", __FUNCTION__);
  string input = "  hello world  ";
  string want = "world hello";

  Solution so;
  string got = so.reverseWords(input);
  if (got != want) {
    printf("failed! want:[%s], but got:[%s]\n", want.c_str(), got.c_str());
  } else {
    printf("success!\n");
  }
  string demo = "hello";
  string ret = so.reverse(demo);
  printf("ret:%s\n", ret.c_str());
}

int main() {

  testcase01();

  return 0;
}
